/**
 * 初始化车次核销详情对话框
 */
var TTrainInfoDlg = {
    id: "TTrainCheckTranTable",	//表格id
    table: null,
    tTrainInfoData : {},
    lock: false
};


/**
 * 初始化表格的列
 */
var mA=1
var mB=1
var mC=1
var mD=1
var mE=1
TTrainInfoDlg.initColumn = function () {
    return [
        {title: '运单号', field: 'original_num',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goods_name',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goods_piece',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '送货地址', field: 'receivingAddress',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '提付', field: 'tf',width: 150, visible: true, align: 'center', valign: 'middle'},
        {title: '代收货款', field: 'collection',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '落地费', field: 'ld',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '中转费',event: 'a', field: 'zz',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '送货费',event: 'b', field: 'sh',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '搬运费',event: 'c', field: 'ty',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '通知发货', field: 'receipt',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '回单要求', field: 'receipt',width: 100, visible: true, align: 'center', valign: 'middle'}

    ];
};
TTrainInfoDlg.summary = function() {
   alert()
}

/**
 * 清除数据
 */
TTrainInfoDlg.clearData = function() {
    this.tTrainInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TTrainInfoDlg.set = function(key, val) {
    this.tTrainInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TTrainInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TTrainInfoDlg.close = function() {
    parent.layer.close(window.parent.TTrain.layerIndex);
}

/**
 * 收集数据
 */
TTrainInfoDlg.collectData = function() {
    this
    .set('id')
    .set('cash')
    .set('weixin')
    .set('alipay')
    .set('bank')
    .set('trainNo')
    ;
}

/**
 * 提交添加
 */
TTrainInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tTrain/add", function(data){
        Feng.success("添加成功!");
        window.parent.TTrain.table.refresh();
        TTrainInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tTrainInfoData);
    ajax.start();
}
var sc=null
TTrainInfoDlg.refStatus = function() {
    this.set('staus', "3");
    this.set('id', $('#id').val());
    this.set('mon_bal_fee', $('#gz').text());
    sc= layer.confirm('确认核销？', {
        btn: ['取消', '确定']
    }, function(index, layero){
        layer.close(sc);
    }, function(index, layero){
        layer.close(sc);
        if(!TTrainInfoDlg.lock) {
            //防止多次点击
            TTrainInfoDlg.lock = true; // 锁定
            var ajax = new $ax(Feng.ctxPath + "/tTrain/transferHX", function (data) {
                Feng.success("操作成功!");
                window.parent.aTable.refresh();
                TTrainInfoDlg.close();
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set(TTrainInfoDlg.tTrainInfoData);
            ajax.start();

        }
    });
}

/**
 * 提交修改
 */
TTrainInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    var cash=parseFloat($('#cash').val());
    var weixin=parseFloat($('#weixin').val());
    var alipay=parseFloat($('#alipay').val());
    var bank=parseFloat($('#bank').val());
    var ys=parseFloat($('#ys').text());
    if((cash+weixin+alipay+bank)!=ys){
        Feng.error("收支金额不等!");
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tTrain/updateDis", function(data){
        Feng.success("修改成功!");
        window.parent.aTable.refresh();
        TTrainInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tTrainInfoData);
    ajax.start();
}
var mDate=null;
TTrainInfoDlg.collectSum = function(m) {
    if(m){
        mDate=m;
    }
    var sum=0;
    var sum1=0;
    var sum2=0;
    for(var i=0;i<mDate.data.length;i++){
        sum=sum+parseFloat(mDate.data[i].tf)+parseFloat(mDate.data[i].collection)+parseFloat(mDate.data[i].ld)-parseFloat(mDate.data[i].zz)-parseFloat(mDate.data[i].ty)-parseFloat(mDate.data[i].sh);

        if(mDate.data[i].tft==0){
            sum1=sum1+parseFloat(mDate.data[i].tf);
        }else{
            sum2=sum2+parseFloat(mDate.data[i].tf);
        }
        if(mDate.data[i].collectiont==0){
            sum1=sum1+parseFloat(mDate.data[i].collection);
        }else{
            sum2=sum2+parseFloat(mDate.data[i].collection);
        }
        if(mDate.data[i].zzt==0){
            sum1=sum1+parseFloat(mDate.data[i].zz)*-1;
        }else{
            sum2=sum2+parseFloat(mDate.data[i].zz)*-1;
        }
        if(mDate.data[i].sht==0){
            sum1=sum1+parseFloat(mDate.data[i].sh)*-1;
        }else{
            sum2=sum2+parseFloat(mDate.data[i].sh)*-1;
        }
        if(mDate.data[i].sht==0){
            sum1=sum1+parseFloat(mDate.data[i].ty)*-1;
        }else{
            sum2=sum2+parseFloat(mDate.data[i].ty)*-1;
        }
        if(mDate.data[i].ldt==0){
            sum1=sum1+parseFloat(mDate.data[i].ld);
        }else{
            sum2=sum2+parseFloat(mDate.data[i].ld);
        }

    }

    $('#hj').text(sum);
    var result="0";
    $("#xj_money input[type='text']").each(function(){
        result=(parseFloat(result)+parseFloat($(this).val()));
    });
    $('#ys').text(result);
    $('#gz').text(parseFloat($('#hj').html())-parseFloat($('#ys').html()));
}

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TTrainInfoDlg.initColumn();
    aTable = new LYTable(TTrainInfoDlg.id, "/tTrain/detailZZList?batch_no="+$('#trainNo').val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TTrainInfoDlg.table = aTable.initCallBack(mTable,TTrainInfoDlg.collectSum);

    })



});
